package ar.com.nexttome.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import ar.com.nexttome.ErrorActivity;
import ar.com.nexttome.PerfilActivity;
import ar.com.nexttome.R;
import ar.com.nexttome.context.ApplicationContext;
import ar.com.nexttome.entities.Persona;
import ar.com.nexttome.util.ActionBar.Action;
import ar.com.nexttome.util.ActionBar.IntentAction;

public class Tools {

	public static void error(Activity act, Exception e) {
		Intent intent = new Intent(act, ErrorActivity.class);
		act.startActivity(intent);
		act.setContentView(R.layout.error);
	}

	public static void alertReintentar(final Context ctx, String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
		builder.setTitle("Reintentar");
		builder.setMessage(msg);
		builder.setCancelable(false);
		builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				Activity activity = (Activity) ctx;
				Intent intent = new Intent();
				intent.setClass(ctx, ctx.getClass());
				activity.startActivity(intent);
				activity.finish();
			}
		});
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
				Activity activity = (Activity) ctx;
				Intent intent = new Intent();
				intent.setClass(ctx, PerfilActivity.class);
				activity.startActivity(intent);
				activity.finish();
			}
		});

		AlertDialog alert = builder.create();
		alert.show();
	}

	public static void createActionMenu(Activity activity, String title, Class<?> clase, int drawable) {
		final ActionBar actionBar = (ActionBar) activity.findViewById(R.id.actionbar);
		final Action action;
		
		actionBar.setTitle(title);
		action = new IntentAction(activity, new Intent(activity, clase), drawable);
		actionBar.addAction(action);
	}

	public static void cargarPreferencias(Context ctx) {
		SharedPreferences pref = ctx.getSharedPreferences(Constantes.STR_PREFERENCIAS, ctx.MODE_PRIVATE);

		Persona yo = new Persona();
		yo.setId(pref.getString("id", null));
		yo.setNick(pref.getString("nick",""));
		yo.setSexo(pref.getString("sexo","M"));
		yo.setComentario(pref.getString("comentario", ""));
		yo.setFoto(pref.getString("foto","iVBORw0KGgoAAAANSUhEUgAAAD0AAABBCAYAAABigqxTAAAABHNCSVQICAgIfAhkiAAABH9JREFUaIHtm01M21YcwB8hTpwP2yEfTjDFzMliMpowtiRap4ZNtAgVMmlqET1E1TZN4rALl2rniQu77Ljbbr3tMGlHLivS2gv70gQVm0Iz1JRGSRCkhDQZpK12soSirovf+z+bDn5nv7/98//5+X32zCytfIlOEElFEN9WPPI3tx/8QuseVlqBu2UiHpD7BVYYllzyoNcpc6xVQAih+9VmdeXeTpHGPQ2X9nKMfTYtJeLneFXqc8hG3x8hA6W9HGO/kRlMXwh7U4zVwv7X9dMJMUUr0xYaQTuZiAfkrz9687Nx1Z/pRhghhBTRpS5MhzM0nod6pucvDaWmEsFJnLLjqj8zIvHRb1cf/QCZ9d7o5U+wHqgbFudi2Quv+94lieG09brT4b5EZtgr//RXLd86ev6M9LmoSS/OxbIjEp+Aisc5GOFKIvjWgJ+1blYaFRJ5Kt80tLAGY7Ww46o/s3T9fM7LMXbcOKDSXo6x0xI+js9tC35xNTaLWx5M2ssx9qXr53O0hTWkPoecVAQRpyyIdCTkFL7KJT71uW1BiHjdcjHmU3HKgUh/nlWvad1HIznncZiT6YXpcMboDGvYme46Op0QS4/JHkO+YUiIpJOKIJpRrUkhkh6WeFOqNSlE0n6eeeWyjJBBo6yTxpn0aYFIev3hAZWZDdqcZVovtOawaHOW6dPCmbReIiHn6euRxQd4rPEsFJyDERbnYtncxUFdIz0i6TcGOFOWZTQ41iqMSHxiMh7QtShAJP1awGWqNC5E0mbNmHRy0Grv67keW/okNWL3tut5Pdf/L35Ztzd2NvVcjy1dKDf320+f/41bHopSrVUslJvGVG+EENqsNnS9YRp8/1v5rt4yRNK37hTvmpnt3cZRBWfQQyRdKDf3V/7Y0f2mofjxz12szTjEDdl3P5fWSWPgsrxW1tVqaxBL7x20D7eqT7BuDnFvnHIgv6zfi3XDG7SNUh27hoFIrxZ2DZ9BuXWniN2WgEgXys393cZRBSJWN2yU6ut6/83HAeuR/bpVM6xBI8kyQoDSRrXipFlGCFDaqFacNMsIAQ84aLfiW9UnedIsIwQsvbxWztPslm4/blUh4oBK7x20D7f3WtR+Xw+qTZA/BPh4erPSoCZdP3yG1QPrBFz6frUJUgVfRL/AgszWvFIzJ1A7H8ClobLxInxu28mUHpboTQsHeRZkcQFcOiq6o9AxNXxuW5Bk968GqHRSEcRujyvgcmU0hLUf9Dig0rzLRlUYIYTGZJ64JoFKvxP2UKvaGoroUkkXGkD3e48OCobsE12YimRJyoNJ38gMpml/zxpSn0PWuzx7HBDpiXhAHlf9VM5Q/RsfjAUvm7azP6kI4vz7Q9dI4+iFsVrYmzPR3IfJkO52hOiIUlIRxJsz0ZxR1bqTXkuPdVQWRuIyJzzca1Zrje6mhLGl5y8NpT5+b2i219Jj+sncAGcPTsbFdDrsEQMCa222nx6+7AXofuBIyCksTEWyZp2IfRmK6FIV0aVeTfWjUq1VXC08Xl9eK+c7FwX+AVmoQdkojnkuAAAAAElFTkSuQmCC"));

		((ApplicationContext) ctx).setCurrentUser(yo);		
	}

	public static boolean existo(Context ctx) {
		SharedPreferences pref = ctx.getSharedPreferences(Constantes.STR_PREFERENCIAS, ctx.MODE_PRIVATE);

		return pref.getBoolean("existo",false);
	}

	public static void guardarPreferencias(Context ctx, Persona yo) {
		SharedPreferences pref = ctx.getSharedPreferences(Constantes.STR_PREFERENCIAS, ctx.MODE_PRIVATE);

		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean("existo", true);
		editor.putString("id"  , yo.getId());
		editor.putString("nick", yo.getNick());
		editor.putString("sexo", yo.getSexo());
		editor.putString("foto", yo.getFoto());
		editor.putString("comentario", yo.getComentario());

		((ApplicationContext) ctx).setCurrentUser(yo);
		
		editor.commit();
	}

	public static Bitmap base64ToBitmap(String encodedFoto) {
		try {
			byte[] imageAsBytes = Base64.decode(encodedFoto.getBytes());
			return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
		} catch(IOException e) {
			return null;
			//devulevo un Bitmap fake?
		}
	}

	public static String bitmapToBase64(Bitmap bitmap) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
		byte[] byteArray = stream.toByteArray();

		return Base64.encodeBytes(byteArray);
	}

	public static Bitmap fileToBitmap(Activity activity, String imgFilePath) {
		try {
			return BitmapFactory.decodeStream(activity.getResources().getAssets().open(imgFilePath));
		} catch (IOException e) {
			return null;
			//devulevo un Bitmap fake?
		}
	}

	public static boolean hayConexion(Activity activity) {
		ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

		return activeNetworkInfo != null;
	}
}